package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.*;
import com.hqyj.pojo.*;
import com.hqyj.service.TeachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p></p>
 *
 * @author : X_jay
 * @date : 2020-09-01 15:50
 **/
@Service
public class TeachServiceImpl implements TeachService {
    @Autowired
    TeachMapper teachMapper;
    @Autowired
    ClassesMapper classesMapper;
    @Autowired
    CourceMapper courceMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoomMapper roomMapper;
    @Override
    public HashMap<String,Object> selectTimeTable(TimeTable table) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //1 设置分页参数：页码和条数
        PageHelper.startPage(table.getPage(),table.getRow());
        //2 查询结果集合
        List<TimeTable> list = teachMapper.selectTimeTable(table.getClassID());
        // 3 创建分页对象
        PageInfo<TimeTable> pageInfo = new PageInfo<TimeTable>(list);

        System.out.println("总条数="+pageInfo.getTotal());
        System.out.println("总页数="+pageInfo.getPages());
        System.out.println("上一页="+pageInfo.getPrePage());
        System.out.println("下一页="+pageInfo.getNextPage());
        System.out.println("首页="+pageInfo.getNavigateFirstPage());
        System.out.println("尾页="+pageInfo.getNavigateLastPage());
        System.out.println("当前页="+table.getPage());
        List<TimeTable> listResult = pageInfo.getList();
        for(TimeTable e:listResult){
            System.out.println(e.toString());
        }
        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        map.put("endPage",pageInfo.getNavigateLastPage());
        map.put("curPage",table.getPage());
        map.put("data",pageInfo.getList());
        return map;
    }

    @Override
    public HashMap<String, Object> add(Teach teach) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //判断时间是否冲突
        Teach teach1 = new Teach();
        teach1.setClId(teach.getClId());
        teach1.setcId(teach.getcId());
        teach1.setuId(teach.getuId());
        teach1.setSkRoom(teach.getSkRoom());
        List<Teach> teaches = teachMapper.selectByTeach(teach1);
        int num=0;
        for (Teach teache : teaches) {
            if (teach.getSkTime().equals(teache.getSkTime())){
                map.put("info","新增失败,本堂课程已经安排，请选择其他课程！");
                return map;
            }else {
                num++;
            }
        }
        //判断空间是否冲突
        Teach teach2 = new Teach();
        teach2.setClId(teach.getClId());
        teach2.setcId(teach.getcId());
        teach2.setuId(teach.getuId());
        teach2.setSkTime(teach.getSkTime());
        List<Teach> teache22 = teachMapper.selectByTeach(teach2);
        //判断老师是否冲突
        Teach teach3 = new Teach();
        teach3.setClId(teach.getClId());
        teach3.setcId(teach.getcId());
        teach3.setSkRoom(teach.getSkRoom());
        teach3.setSkTime(teach.getSkTime());
        List<Teach> teache33 = teachMapper.selectByTeach(teach3);
        if (teache22.size()>0){
            map.put("info","新增失败,该老师当天已有其他课程，请选择其他时间！");
            return map;
        }else if (teache33.size()>0){
            map.put("info","新增失败,改班级当天课程已安排了老师，请选择其他时间！");
            return map;
        } else if(num==teaches.size()&&teachMapper.insertSelective(teach)>0){
            map.put("info","新增成功");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> selectClass() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Classes> classes = classesMapper.selectAllClasses();
        for (Classes aClass : classes) {
            System.out.println(aClass.toString());
        }
        map.put("data",classes);
        return map;
    }

    @Override
    public HashMap<String, Object> selectCource(Long clId) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Classes classes = classesMapper.selectByPrimaryKey(clId);
        System.out.println("234567890-==-098767890-=-098767890"+classes.toString());
        List<Cource> cources = courceMapper.selectByDId(classes.getdId());
        for (Cource cource : cources) {
            System.out.println("cource = " + cource);
        }
        map.put("data",cources);
        return map;
    }

    @Override
    public HashMap<String, Object> selectTeacherList() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<User> users = userMapper.selectTeacherList(3);
        for (User user : users) {
            System.out.println("user = " + user);
        }
        map.put("data",users);
        return map;
    }

    @Override
    public HashMap<String, Object> selectroomList() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Room> rooms = roomMapper.selectAllRoom();
        for (Room room : rooms) {
            System.out.println("room = " + room);
        }
        map.put("data",rooms);
        return map;
    }

    @Override
    public HashMap<String, Object> updateByTeach(Teach record) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = teachMapper.updateByPrimaryKey(record);
        if (i>0){
            map.put("info","修改成功");
        }else {
            map.put("info","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> deleteBySkId(Long skId) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = teachMapper.logicDelete(skId);
        if (i>0){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }
}
